package server.multiServlet;

import com.google.common.collect.Lists;
import lombok.Data;
import server.HttpProtocolUtil;
import server.Request;
import server.Response;

import java.io.IOException;
import java.util.List;

@Data
public class Engine implements Container {
    private List<Host> mapperHosts = Lists.newArrayList();

    @Override
    public void init() {
        mapperHosts.forEach(Host::init);
    }

    @Override
    public void execute(Request request, Response response) {
        for (Host mapperHost : mapperHosts) {
            if (mapperHost.getName().equals(request.getHost())) {
                mapperHost.execute(request, response);
                return;
            }
        }

        try {
            response.output(HttpProtocolUtil.getHttpHeader404());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
